In [12]:
from __future__ import division
from sympy import *

x, y, z, t = symbols('x y z t')
k, m, n = symbols('k m n', integer=True)
f, g, h = symbols('f g h', cls=Function)

init_printing(use_latex='mathjax') # this makes the output look like real math symbols

In [14]:
diff(x**4 + cos(x**2), x)


Out[14]:
$$4 x^{3} - 2 x \sin{\left (x^{2} \right )}$$

In [15]:
integrate(x**4+ cos(x**2), x)


Out[15]:
$$\frac{x^{5}}{5} + \frac{\sqrt{2} \sqrt{\pi} C\left(\frac{\sqrt{2} x}{\sqrt{\pi}}\right) \Gamma{\left(\frac{1}{4} \right)}}{8 \Gamma{\left(\frac{5}{4} \right)}}$$

In [17]:
test = integrate(x**4+ cos(x**2)*sin(x**2), x)

In [18]:
test


Out[18]:
$$\frac{x^{5}}{5} - \frac{3 \sqrt{\pi} S\left(\frac{2 x}{\sqrt{\pi}}\right)}{16 \Gamma{\left(\frac{1}{4} \right)}} \Gamma{\left(- \frac{3}{4} \right)}$$

In [19]:
sqrt(8)


Out[19]:
$$2 \sqrt{2}$$

In [20]:
x+y**2


Out[20]:
$$x + y^{2}$$

In [21]:
I**2


Out[21]:
$$-1$$

In [22]:
Rational(1, 55)


Out[22]:
$$\frac{1}{55}$$

In [25]:
rat = Rational(1, 55) + Rational(58, 327)
rat


Out[25]:
$$\frac{3517}{17985}$$

N() does a numarica eval


In [26]:
N(rat)


Out[26]:
$$0.195551848762858$$

In [28]:
N(pi, 1000) # 1000 is how many decimals I want


Out[28]:
$$3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420198$$

In [30]:
expr = x**2 + 2*x + 1
expr


Out[30]:
$$x^{2} + 2 x + 1$$

In [31]:
expr.subs(x, 10)


Out[31]:
$$121$$

In [33]:
expr = pi * x**2
expr


Out[33]:
$$\pi x^{2}$$

In [40]:
expr.subs(x, 10)


Out[40]:
$$100 \pi$$

In [41]:
N(_)


Out[41]:
$$314.159265358979$$

In [51]:
expr = (x+y + 5)**10
expr


Out[51]:
$$\left(x + y + 5\right)^{10}$$

In [52]:
expand(expr)


Out[52]:
$$x^{10} + 10 x^{9} y + 50 x^{9} + 45 x^{8} y^{2} + 450 x^{8} y + 1125 x^{8} + 120 x^{7} y^{3} + 1800 x^{7} y^{2} + 9000 x^{7} y + 15000 x^{7} + 210 x^{6} y^{4} + 4200 x^{6} y^{3} + 31500 x^{6} y^{2} + 105000 x^{6} y + 131250 x^{6} + 252 x^{5} y^{5} + 6300 x^{5} y^{4} + 63000 x^{5} y^{3} + 315000 x^{5} y^{2} + 787500 x^{5} y + 787500 x^{5} + 210 x^{4} y^{6} + 6300 x^{4} y^{5} + 78750 x^{4} y^{4} + 525000 x^{4} y^{3} + 1968750 x^{4} y^{2} + 3937500 x^{4} y + 3281250 x^{4} + 120 x^{3} y^{7} + 4200 x^{3} y^{6} + 63000 x^{3} y^{5} + 525000 x^{3} y^{4} + 2625000 x^{3} y^{3} + 7875000 x^{3} y^{2} + 13125000 x^{3} y + 9375000 x^{3} + 45 x^{2} y^{8} + 1800 x^{2} y^{7} + 31500 x^{2} y^{6} + 315000 x^{2} y^{5} + 1968750 x^{2} y^{4} + 7875000 x^{2} y^{3} + 19687500 x^{2} y^{2} + 28125000 x^{2} y + 17578125 x^{2} + 10 x y^{9} + 450 x y^{8} + 9000 x y^{7} + 105000 x y^{6} + 787500 x y^{5} + 3937500 x y^{4} + 13125000 x y^{3} + 28125000 x y^{2} + 35156250 x y + 19531250 x + y^{10} + 50 y^{9} + 1125 y^{8} + 15000 y^{7} + 131250 y^{6} + 787500 y^{5} + 3281250 y^{4} + 9375000 y^{3} + 17578125 y^{2} + 19531250 y + 9765625$$

In [53]:
factor(_)


Out[53]:
$$\left(x + y + 5\right)^{10}$$

In [59]:
new_expr = integrate(_, x)
new_expr


Out[59]:
$$\frac{x^{12}}{132} + x^{11} \left(\frac{y}{11} + \frac{5}{11}\right) + x^{10} \left(\frac{y^{2}}{2} + 5 y + \frac{25}{2}\right) + x^{9} \left(\frac{5 y^{3}}{3} + 25 y^{2} + 125 y + \frac{625}{3}\right) + x^{8} \left(\frac{15 y^{4}}{4} + 75 y^{3} + \frac{1125 y^{2}}{2} + 1875 y + \frac{9375}{4}\right) + x^{7} \left(6 y^{5} + 150 y^{4} + 1500 y^{3} + 7500 y^{2} + 18750 y + 18750\right) + x^{6} \left(7 y^{6} + 210 y^{5} + 2625 y^{4} + 17500 y^{3} + 65625 y^{2} + 131250 y + 109375\right) + x^{5} \left(6 y^{7} + 210 y^{6} + 3150 y^{5} + 26250 y^{4} + 131250 y^{3} + 393750 y^{2} + 656250 y + 468750\right) + x^{4} \left(\frac{15 y^{8}}{4} + 150 y^{7} + 2625 y^{6} + 26250 y^{5} + \frac{328125 y^{4}}{2} + 656250 y^{3} + 1640625 y^{2} + 2343750 y + \frac{5859375}{4}\right) + x^{3} \left(\frac{5 y^{9}}{3} + 75 y^{8} + 1500 y^{7} + 17500 y^{6} + 131250 y^{5} + 656250 y^{4} + 2187500 y^{3} + 4687500 y^{2} + 5859375 y + \frac{9765625}{3}\right) + x^{2} \left(\frac{y^{10}}{2} + 25 y^{9} + \frac{1125 y^{8}}{2} + 7500 y^{7} + 65625 y^{6} + 393750 y^{5} + 1640625 y^{4} + 4687500 y^{3} + \frac{17578125 y^{2}}{2} + 9765625 y + \frac{9765625}{2}\right)$$

In [62]:
temp = new_expr.subs(x, 1)
temp.subs(y, 2)


Out[62]:
$$\frac{10383422873}{44}$$

In [65]:



Out[65]:
$$\frac{y^{10}}{2} + \frac{80 y^{9}}{3} + \frac{2565 y^{8}}{4} + 9156 y^{7} + 85967 y^{6} + 554616 y^{5} + \frac{9959865 y^{4}}{4} + \frac{23044730 y^{3}}{3} + \frac{31169301 y^{2}}{2} + \frac{206547056 y}{11} + \frac{1346704211}{132}$$

In [66]:
sin(x)/cos(x)


Out[66]:
$$\frac{\sin{\left (x \right )}}{\cos{\left (x \right )}}$$

In [67]:
simplify(_)


Out[67]:
$$\tan{\left (x \right )}$$

In [70]:
expr = 1/(x**2 + 2*x)
expr


Out[70]:
$$\frac{1}{x^{2} + 2 x}$$

In [71]:
apart(expr)


Out[71]:
$$- \frac{1}{2 x + 4} + \frac{1}{2 x}$$

In [72]:
together(_)


Out[72]:
$$\frac{1}{x \left(x + 2\right)}$$

In [73]:
diff(sin(x), x)


Out[73]:
$$\cos{\left (x \right )}$$

In [74]:
Integral(sin(x), (x, 0, pi))


Out[74]:
$$\int_{0}^{\pi} \sin{\left (x \right )}\, dx$$

In [75]:
N(_)


Out[75]:
$$2.0$$

In [77]:
expr = Sum(1/(x**2 + 2*x), (x, 1, 10))
expr


Out[77]:
$$\sum_{x=1}^{10} \frac{1}{x^{2} + 2 x}$$

In [78]:
N(_)


Out[78]:
$$0.662878787878788$$

In [79]:
expr.doit()


Out[79]:
$$\frac{175}{264}$$

In [81]:
expr = Product(1/(x**2 + 2*x), (x, 1, 10))
expr


Out[81]:
$$\prod_{x=1}^{10} \frac{1}{x^{2} + 2 x}$$

In [82]:
expr.doit()


Out[82]:
$$\frac{1}{869100503040000}$$

In [84]:
expr = 2*x + 10
expr


Out[84]:
$$2 x + 10$$

In [85]:
solve(expr)


Out[85]:
$$\left [ -5\right ]$$

In [87]:
expr = x**2 - 10
expr


Out[87]:
$$x^{2} - 10$$

In [88]:
solve(expr)


Out[88]:
$$\left [ - \sqrt{10}, \quad \sqrt{10}\right ]$$

Solve systems of equations!!! :D


In [90]:
expr_1 = 2*x + y
expr_2 = 2*y - x + 21

In [91]:
solve([expr_1, expr_2], (x, y))


Out[91]:
$$\left \{ x : \frac{21}{5}, \quad y : - \frac{42}{5}\right \}$$

Units


In [92]:
from sympy.physics import units as u

In [93]:
5. * u.microgram


Out[93]:
$$5.0 \cdot 10^{-9} kg$$

In [94]:
1./5 * u.inch


Out[94]:
$$0.00508 m$$

In [96]:
1. * u.nano


Out[96]:
$$1.0 \cdot 10^{-9}$$

In [97]:
u.watt


Out[97]:
$$\frac{kg m^{2}}{s^{3}}$$

In [99]:
1. * u.watts


Out[99]:
$$\frac{1.0 m^{2}}{s^{3}} kg$$

In [100]:



Out[100]:
$$\frac{kg}{602214179000000000000000000}$$

In [101]:
kmph = u.km / u.hour
mph = u.mile / u.hour

N(mph/kmph)


Out[101]:
$$1.609344$$

Plotting


In [102]:
from sympy.plotting import plot, plot_parametric

In [105]:
expr = sin(2*sin(x**3))
plot(expr, (x, 0, 5))


Out[105]:
<sympy.plotting.plot.Plot at 0x106124630>

In [106]:
expr = diff(x**5 + sin(x), x)

In [107]:
expr


Out[107]:
$$5 x^{4} + \cos{\left (x \right )}$$

In [108]:
plot(expr, (x, -5, 5))


Out[108]:
<sympy.plotting.plot.Plot at 0x1080f5d68>

In [109]:
def sympy_expr(x_val):
    expr = x**2 + sqrt(3)*x - Rational(1, 3)
    return expr.subs(x, x_val)
sympy_expr(3)


Out[109]:
$$3 \sqrt{3} + \frac{26}{3}$$

In [110]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

In [111]:
list1 = np.arange(1, 1000)
list2 = pd.Series(list1)

In [112]:
%timeit [sympy_expr(item) for item in list1]
%timeit [sympy_expr(item) for item in list2]


1 loop, best of 3: 618 ms per loop
1 loop, best of 3: 633 ms per loop

In [113]:
%timeit np.vectorize(sympy_expr)(list1)
%timeit list2.apply(sympy_expr)


1 loop, best of 3: 455 ms per loop
1 loop, best of 3: 450 ms per loop

In [114]:
expr = x**2 + sqrt(3)*x - Rational(1, 3)
lf = lambdify(x, expr)

In [115]:
%timeit lf(list1)
%timeit lf(list2)


The slowest run took 43.16 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 7.35 µs per loop
1000 loops, best of 3: 282 µs per loop

In [116]:
fig = plt.figure()
axes = fig.add_subplot(111)

x_vals = np.linspace(-5., 5.)
y_vals = lf(x_vals)

axes.grid()
axes.plot(x_vals, y_vals)

plt.show()



In [ ]: